﻿Imports System.Data.SqlClient

Public Class frmNominasIngreso

    Private Sub frmNominasIngreso_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        CargaIngresos()
        Inicializa()

    End Sub
    Sub Inicializa()
        datFecha.Value = Now()
        txtIngreso.Text = 0
        txtIngreso.TextAlign = HorizontalAlignment.Right
    End Sub
    Sub CargaIngresos()
        dgIngreso.Rows.Clear()

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "SELECT * FROM NominasIngreso ORDER BY Fecha DESC"

        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader
        Dim Fecha As Date

        Do While rdBuscar.Read
            Fecha = rdBuscar("Fecha")
            dgIngreso.Rows.Add(rdBuscar("IdIngreso"), Fecha.ToShortDateString, rdBuscar("Ingreso"))
        Loop

        dgIngreso.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        dgIngreso.Columns(2).DefaultCellStyle.Format = "N2"

    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Dim IdIngreso As Integer = dgIngreso.CurrentRow.Cells(0).Value
        Dim Status As Integer = BuscarIntDatoCitra("SELECT Status FROM NominasIngreso WHERE IdIngreso=" & IdIngreso)

        If Status = 0 Then
            Call SetSqlCitra("DELETE FROM NominasIngreso WHERE IdIngreso=" & IdIngreso)
            CargaIngresos()
        Else
            MsgBox("El registro pertenece a una nomina ya procesada, no se puede eliminar", MsgBoxStyle.Critical, "Aviso")
        End If
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        If txtIngreso.Text = "" Then
            MsgBox("Debes de indicar el ingreso", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        If txtIngreso.Text = 0 Then
            MsgBox("0 no es una cantidad valida", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        Dim Fecha As String = datFecha.Value.ToString("MM/dd/yyyy")
        Dim IdIngreso As Integer = BuscarIntDatoCitra("SELECT IdIngreso FROM NominasIngreso WHERE CONVERT(VARCHAR,Fecha,101)='" & Fecha & "'")
        If IdIngreso > 0 Then
            MsgBox("Ya existe un registro con la fecha indicada, si desea agregarlo, debe eliminar el registro existente", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        Dim Ingreso As New clsNominasIngreso
        Ingreso.Fecha = datFecha.Value
        Ingreso.Ingreso = txtIngreso.Text
        Ingreso.Status = 0
        Ingreso.Insertar()

        CargaIngresos()
        Inicializa()

    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub
End Class